/*
 * AlteraRevistaAction.java
 *
 * Created on 26 de Julho de 2007, 02:04
 */

package br.usp.poli.mfc.struts.action;

import br.usp.poli.mfc.struts.DAO.DAOFactory;
import br.usp.poli.mfc.struts.DAO.RevistaDAO;
import br.usp.poli.mfc.struts.actionform.AlteraRevistaActionForm;
import br.usp.poli.mfc.struts.bean.RevistaxGenero;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
 *
 * @author Renato
 * @version
 */

public class AlteraRevistaAction extends Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String ERROR = "error";
    private final static String SUCCESS1 = "success1";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int _idRevista = Integer.parseInt(request.getParameter("idRevista"));
        int _idGenero = Integer.parseInt(request.getParameter("idGenero"));
        AlteraRevistaActionForm frmAltRev = (AlteraRevistaActionForm) form;
        RevistaxGenero _revistaxgenero = null;
        RevistaDAO _revistaDAO = DAOFactory.getRevistaDAO();
        if(frmAltRev.getTitulo() != null){
            try{
                System.out.println("nao null");
                //SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); // MUDA AQUI se quiser... ou tem outro jeito...
                //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // SOH PARA DEBUG!!! MUDAR DEPOIS
                //Date myDate = dateFormat.parse(frmAltRev.getDatapublicacao());
                //Calendar c = Calendar.getInstance();
                //c.setTime(frmAltRev.get);
                _revistaxgenero = new RevistaxGenero();
                _revistaxgenero.setIDRevista(_idRevista);
                _revistaxgenero.setIDGenero(_idGenero);
                _revistaxgenero.setTitulo(frmAltRev.getTitulo());
                //_revistaxgenero.setGenero(frmAltRev.getGenero()); // Isso eh aqui eh string? mas vc tah passando um valor de "1" (1 string)... nao faz sentido nenhum
                _revistaxgenero.setDescricao(frmAltRev.getDescricao());
                _revistaxgenero.setDatapublicacao(frmAltRev.getDatapublicacao());
                if(_revistaDAO.alteraRevistaByID(_revistaxgenero))
                    return mapping.findForward(SUCCESS1);
                else
                    return mapping.findForward(ERROR);
            } catch(Exception e){
                e.printStackTrace();
            }
        } else{
            System.out.println("null");
            _revistaxgenero = _revistaDAO.getRevistaByID(_idRevista);
            HttpSession ysession = request.getSession();
            ysession.setAttribute("viewRevista",_revistaxgenero);
            return mapping.findForward(SUCCESS);
        }
        return mapping.findForward(ERROR);
        
    }
}
